package com.toocms.jinjuhang.ui.mine.personal_data.account

import android.app.Application
import android.os.CountDownTimer
import android.text.TextUtils
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class EditAccountModel(application: Application) : BaseViewModel<BaseModel>(application) {

    var timer: Timer? = null
    val accountObservableField = ObservableField<String>()
    val verifyObservableField = ObservableField<String>()
    val isCanGetVerifyCodeObservableBoolean = ObservableBoolean(true)
    val getVerifyCodeHintObservableField by lazy {
        ObservableField(StringUtils.getString(R.string.str_get_verify_code))
    }

    val getVerifyCodeClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        val account = accountObservableField.get()
        if (TextUtils.isEmpty(account)) {
            showToast(R.string.str_no_input_login_mobile_hint)
            return@BindingAction
        }
        val params = mutableMapOf<String, String?>()
        params["account"] = account
        params["unique_code"] = Constants.UniqueCode.REGISTER
        requestData(
            Urls.Verify.GET_VERIFY,
            params,
            resultClass = String::class.java,
            isShowLoading = true,
            result = {
                showToast(it)
                timer?.cancel()
                timer = Timer()
                timer?.start()
            })
    })
    val immediatelyVerifyClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["account"] = accountObservableField.get()
        params["verify"] = verifyObservableField.get()
        requestData(Urls.Login.CHANGE_ACCOUNT, params, resultClass = String::class.java, result = {
            showToast(it)
            finishFragment()
        })
    })

    //------------------------------------------------内部类-----------------------------------------

    inner class Timer : CountDownTimer(
        Constants.Extra.REGAIN_VERIFY_CODE_TIME,
        Constants.Extra.COUNT_DOWN_INTERVAL
    ) {
        override fun onTick(millisUntilFinished: Long) {
            isCanGetVerifyCodeObservableBoolean.set(false)
            getVerifyCodeHintObservableField.set("${StringUtils.getString(R.string.str_regain)}(${millisUntilFinished / 1000}s)")
        }

        override fun onFinish() {
            isCanGetVerifyCodeObservableBoolean.set(true)
            getVerifyCodeHintObservableField.set("${StringUtils.getString(R.string.str_regain)}")
        }
    }
}